/**
 * @Author: yuanmanxue
 * @Date:   2017-09-27 03:26:29
 * @Last modified by:   yuanmanxue
 * @Last modified time: 2018-01-12 04:57:57
 */
// js异步函数执行机制
{
  (function(){
    var arr=[];
    for(let i=0;i<3;i++){
      var cale=function(){
        return i*i
      }
      arr.push(cale);
    };
    for(let i=0;i<arr.length;i++){
      console.log(arr[i]());//0,1,4
    }
  })()
}

{
    (function(){
      let arr=[];
      for(let i=0;i<3;i++){
        var param=i;
        var calc=function(){
          return param*param
        }
        arr.push(calc)
      }
      for(let i=0;i<arr.length;i++){
        console.log(arr[i]());//4,4,4
      }
    })()
}

{
  //异步函数 ---1
  async function async1(){
    console.log('async1 start');
    //在这里返回了一个Promise异步函数  ---2
    //最后调用了  await这个异步函数里面的异步函数 async2  ---3
    await async2();
    console.log('async1 end');
  }
  async function async2(){
    console.log('async2');
  }
  console.log('script start');
  //最后自动执行这个延迟函数 ---4
  setTimeout(function () {
    console.log('setTimeout');
  }, 0);
  async1();
  new Promise(function(resolve){
    console.log('promise1');
    resolve();
  }).then(function(){
    console.log('promise 2');
  })
  console.log('script end');

/**
 * script start   同步函数
 * async1 start  async1函数是同步函数调用  async异步async1  遇到await以后先返回调用一个 Promise函数
 * async2
 * promise1
 * script end
 * promise 2
 * async1 end
 * setTimeout
 *在异步调用的时候是.then()方法优先于await()  异步函数的调用顺序是跟异步函数调用顺序有关还是和函数调用有关
 */

}

{
  // console.log('======================');
  /**
   * [getSum 有一个数组data，其值是一系列的无序，不重复的数字，取出n个，使其和sum。写出函数的算法，并说明其时间复杂度和空间复杂度，写出一个解即可。]
   * @param  {[type]} data [description]
   * @param  {[type]} n    [description]
   * @param  {[type]} sum  [description]
   * @return {[type]}      [description]
   */
  function  getSum(data,n,sum){

  }
}

{
  setTimeout(function() {
    console.log('HHHHHH');
  },0)
  for(var i = 0; i < 3; i++) {
     setTimeout(function() {
         console.log('timeout' + i);
     })
  }
  new Promise(function(resolve) {
      console.log('promise1');
      for(var i = 0; i < 1000; i++) {
          i == 99 && resolve();
      }
      console.log('promise2');
  }).then(function() {
      console.log('then1');
  })
      console.log('global1');
  /**
   * promise1
   * promise2
   * global1
   * then1
   * timeout3  * 3
   */
   console.log('======================');
}

{
  /**
   * [getJson description]   用 Promise 封装ajax
    * @param  {[type]} url [description]
   * @return {[type]}     [description]
   */
  function getJson(url) {
    return new Promise((resolve,reject) => {
      let xml = new XMLHttpRequest();
      xml.open('GET',url,true);
      xml.send();
      xml.onreadystatechange = function(){
        if(xml.readyState === 4) {
          if(xml.status === 200) {
              try{
                let ret = JSON.parse(xml.responseText);
                resolve(ret);
              } catch (e){
                reject(e);
              }
          } else {
            reject(new Error(xml.statusText));
          }
        }
      }
    })
  }
  var url = 'https://hq.tigerbrokers.com/fundamental/finance_calendar/getType/2017-02-26/2017-06-10';
  getJson(url)
  .then(function(res){
    console.log(res);
  })
}
